package cn.com.sbell.vss.csg.vap.util;

import cn.com.sbell.vss.ICommander;
import cn.com.sbell.vss.Response;
import cn.com.sbell.vss.VssConstants;
import cn.com.sbell.vss.VissSession;

import cn.com.sbell.vss.VssException;
import cn.com.sbell.vss.auth.InterDomainAuth;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class RouteSessionMapper {
	
	private static  Map routeSessionMap=Collections.synchronizedMap(new HashMap());
	private static InterDomainAuth interDomainAuth;
	private static Log logger = LogFactory.getLog(RouteSessionMapper.class);
//	(domainUsername,domainPassword)
	public static CrossDomainCredential getRoute(String nextCSG,HttpServletRequest request)throws VssException
	{
		CrossDomainCredential nextSessionCred=(CrossDomainCredential)routeSessionMap.get(nextCSG);
		VissSession sessionData=(VissSession)request.getAttribute(VssConstants.KEY_USER_SESSION_DATA);
		String sessionId=null;
		if(null!=sessionData){
			sessionId=sessionData.getNextHopSessionId(nextCSG);
		}
		if(null!=nextSessionCred)
		{
			if(null!=sessionId)
			{
				nextSessionCred.setNextDomainSessionId(sessionId);
			}
			logger.debug("find existing nextSessionId:"+nextSessionCred);
			return nextSessionCred;
		}else
		{
			Response response=interDomainAuth.execute(request);
			
			String domainUsername=(String)((Map)response.getParameters()).get("nextDomainUsername");
			String domainPassword=(String)((Map)response.getParameters()).get("nextDomainPassword");
			
			if((null!=domainUsername)&&(null!=domainPassword))
			{
				sessionData.removeNextHopSessionId(nextCSG);
				logger.debug("get route successfully obtained next domain username, password:"+domainUsername+","+domainPassword);
				return new CrossDomainCredential(domainUsername,domainPassword);
			}
			
		}
		logger.debug("fail to obtain sessionid or username password");
		return null;
		
	}
	public static CrossDomainCredential checkRoute(String nextCSG,HttpServletRequest request)
	{
		return (CrossDomainCredential)routeSessionMap.get(nextCSG);
	}
	public static CrossDomainCredential refreshRoute(String nextCSG,HttpServletRequest request)
	{
		routeSessionMap.remove(nextCSG);
		VissSession sessionData=(VissSession)request.getAttribute(VssConstants.KEY_USER_SESSION_DATA);
		if(null!=sessionData){
		sessionData.removeNextHopSessionId(nextCSG);
		}
		Response response=interDomainAuth.execute(request);
		
		String domainUsername=(String)((Map)response.getParameters()).get("nextDomainUsername");
		String domainPassword=(String)((Map)response.getParameters()).get("nextDomainPassword");
		
		if((null!=domainUsername)&&(null!=domainPassword))
		{
			logger.debug("refresh route successfully obtained next domain username, password:"+domainUsername+","+domainPassword);
			return new CrossDomainCredential(domainUsername,domainPassword);
		}
		return null;
		
	}
	public static void updateRoute(String nextCSG, CrossDomainCredential nextSessionCred,HttpServletRequest request)
	{
		routeSessionMap.put(nextCSG, nextSessionCred);
		VissSession sessionData=(VissSession)request.getAttribute(VssConstants.KEY_USER_SESSION_DATA);
		if(null!=sessionData){
		sessionData.putNextHopSessionId(nextCSG, nextSessionCred.getNextDomainSessionId());
		}
	}
	public static void removeRoute(String nextCSG,HttpServletRequest request)
	{
		routeSessionMap.remove(nextCSG);
		VissSession sessionData=(VissSession)request.getAttribute(VssConstants.KEY_USER_SESSION_DATA);
		if(null!=sessionData){
		sessionData.removeNextHopSessionId(nextCSG);
		}
	}
	
	public  InterDomainAuth getInterDomainAuth() {
		return interDomainAuth;
	}
	public  void setInterDomainAuth(InterDomainAuth interDomainAuth) {
		RouteSessionMapper.interDomainAuth = interDomainAuth;
	}
	
}
